//
//  FirstViewController.m
//  FIrst
//
//  Created by RAMBO on 15/4/10.
//  Copyright (c) 2015年 Myfuture. All rights reserved.
//

#import "FirstViewController.h"
#import "MyConnection.h"
#import "CustomCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "DisplayViewController.h"
#import "DetailDailyViewController.h"


#define HTTPLOG @"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface FirstViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) UICollectionView *myCollectionView;
@end

@implementation FirstViewController

- (void)dealloc
{
    [_tableArray release];
    [_myCollectionView release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatInterface];
    [self startConnect];
    
}

#pragma mark - 
#pragma mark 创建界面 - collectionView
- (void)creatInterface
{
    UICollectionViewFlowLayout *flowOut = [[UICollectionViewFlowLayout alloc] init];
    flowOut.itemSize = CGSizeMake(WIDTH - 10, HEIGHT - 64 - 49);
    [flowOut setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , HEIGHT) collectionViewLayout:flowOut];
    [self.view addSubview:_myCollectionView];
    [_myCollectionView release];
    [flowOut release];
    
    // 这个地方存放努力加载的图片 (有待更改的地方)
    UIImageView *image = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [image setImage:[UIImage imageNamed:@"back.jpg"]];
    [_myCollectionView setBackgroundView:image];
    
    [_myCollectionView setDelegate:self];
    [_myCollectionView setDataSource:self];
    [_myCollectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"collect"];
    _myCollectionView.autoresizesSubviews = YES;
    _myCollectionView.pagingEnabled = YES;
    _myCollectionView.showsHorizontalScrollIndicator = NO;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_rightButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(turnOther)];
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

#pragma mark - 
#pragma mark 跳转进入展示页面

- (void)turnOther
{
    DisplayViewController *displ = [[DisplayViewController alloc] init];
    displ.disArray = self.tableArray;
    displ.count = 1;
    [self.navigationController pushViewController:displ animated:YES];
    [displ release];
}

#pragma mark -
#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
#warning 最后一个小模块显示不出来, 是一个小的BUG, 有待修改
    if (indexPath.row == [_tableArray count] - 1) {
        return nil;
    }
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collect" forIndexPath:indexPath];
    
    NSDictionary *dic = [_tableArray objectAtIndex:indexPath.item];
    
    NSString *s = [dic objectForKey:@"cover_landscape"];
    NSURL *url = [NSURL URLWithString:s];
    [cell.upImageView sd_setImageWithURL:url];
        
    [cell.titleLabel setText:[dic objectForKey:@"title"]];
    [cell.sourceLable setText:[dic objectForKey:@"source"]];
    
    NSString *numS = [dic objectForKey:@"pubdate"];
    NSArray *arr = [numS componentsSeparatedByString:@" "];
    numS = [arr objectAtIndex:1];
    numS = [numS substringToIndex:2];
    NSString *monS = [arr objectAtIndex:0];
    NSString *show = [NSString stringWithFormat:@"%@ %@", numS, monS];
    [cell.numLabel setLineBreakMode:NSLineBreakByWordWrapping];
    cell.numLabel.numberOfLines = 2;
    cell.numLabel.text = show;
    
    return cell;
}

#pragma mark -
#pragma mark 点击跳转到详情界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    DetailDailyViewController *deta = [[DetailDailyViewController alloc] init];
    deta.detailArray = self.tableArray;
    NSInteger click = indexPath.item;
    deta.click = click;
    [self.navigationController pushViewController:deta animated:YES];
    [deta release];
}

#pragma mark - 
#pragma mark 网络请求数据
- (void)startConnect
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"page", @"iphone", @"ver", @"9", @"app_ver", nil];
    [MyConnection connectWithUrl:HTTPLOG parmaters:dic finishBlock:^(NSData *data) {
        self.tableArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [_myCollectionView reloadData];
    }];
}





@end
